package com.tang.thepath.service;


import android.util.Log;
import android.widget.Toast;
import com.tang.thepath.App;
import com.tang.thepath.exception.ApiException;
import rx.Subscriber;


public abstract class HttpSubscriber<T> extends Subscriber<T> {


    public HttpSubscriber(){
    }

    protected abstract void onSuccess(T t);
    protected void onApiError(ApiException e){

    }
    @Override
    public void onCompleted() {
        if (!isUnsubscribed()) {
            unsubscribe();
        }
    }

    @Override
    public void onError(Throwable e) {
        ApiException exception=ApiException.exception(e);
        Toast.makeText(App.getContext(), exception.getMessage(), Toast.LENGTH_SHORT).show();
        Log.e("httpError",exception.getMessage(),exception);
        onApiError(exception);
//        if(e instanceof ApiException){
//            onApiError((ApiException) e);
//        }else{
//            onApiError(ApiException.exception(e));
//        }
    }

    @Override
    public void onNext(T t) {
            onSuccess(t);
    }
}
